package com.jz.yanshe.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.ExpressResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.com.feierman.R;

import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.util.PreferencesHelper;

/**
 * Created by Administrator on 2018/4/16.
 */

public class ExpressListActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    int page = 1;
    String orderid;

    MyAdapter myAdapter = new MyAdapter();
    private List<ExpressResponse.Express> list = new ArrayList<>();


    @Override
    protected void initViews() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 1;
            }
        });
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        swipeToLoadLayout.setLoadMoreEnabled(false);
    }

    @Override
    protected void initData() {
        orderid = getIntent().getStringExtra("orderid");
        getExpressInfo(orderid);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_express_list;
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onRefresh() {
        getExpressInfo(orderid);
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_express_list, parent, false));
            return holder;
        }


        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("express_code",list.get(position).express_code);
                    startActivity(ExpressDetailActivity.class,bundle);
                }
            });
//            ImageLoaderHelper.getInstance().GlideImageLoader(this,list.get(position).,holder.img_goods,R.mipmap.img_hot_default);
            holder.tv_express_name.setText(list.get(position).express_name);
            holder.tv_express_no.setText("快递单号:"+list.get(position).express_code);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv_express_name, tv_express_no;
            ImageView img_goods;

            public Holder(View itemView) {
                super(itemView);
                tv_express_name = itemView.findViewById(R.id.tv_express_name);
                tv_express_no = itemView.findViewById(R.id.tv_express_no);
                img_goods = itemView.findViewById(R.id.img_goods);
            }
        }
    }



    private void getExpressInfo(String order_id) {
        RestProxy.getInstance().getExpressInfo(PreferencesHelper.getInstance().getString(this, "token"), order_id, new Observer<ExpressResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ExpressResponse expressResponse) {
                list.clear();
                list.addAll(expressResponse.list);
                myAdapter.notifyDataSetChanged();
            }


            @Override
            public void onError(Throwable e) {
                swipeToLoadLayout.setRefreshing(false);

            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);

            }
        });
    }
}
